<?php

class Admin_CommentController extends ExtLb_Controller_Action{
	
	private $commentTbl;
	

    public function init()
    {
        $this->loadLayout('admin', 'layout');
        $this->loadCss('admin', 'comment');
        
        $this->commentTbl	= new Admin_Model_Comments();
    }

    public function indexAction()
    {
    	// === Defaul selectComment
    	$selectComment = $this->commentTbl->selectComment();
    	//
    	// === Loc noi dung
    	if (isset($_GET['tbn_filter']) && $_GET['tbn_cate'] != 0){
    		$selectComment = $this->commentTbl->selectComment("datecomment like '%".$_GET['tbn_cate']."%'");
    	}
    	
        //
        // === Exe search
    	if(isset($_GET['search_box_sumit'])){
    		if(empty($_GET['search_box_text'])){
    			$this->jShowMessage("Hãy nhập vào chủ đề cần tìm !!!");
    		}else{
    			$selectComment = $this->commentTbl
    								->selectComment("subject like '%".$_GET['search_box_text']."%'");
    		}
    	}
        
        $paginatorComment = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($selectComment));
        $paginatorComment->setPageRange(3)
        				->setItemCountPerPage(20)
        				->setCurrentPageNumber($this->_getParam('page',1));
        //
        $this->view->paginatorComment = $paginatorComment;
        $this->view->dateFilter	 = $this->getDateComment();
        
        //
        // === Forward action when del
        if(isset($_GET['comment-check']) && isset($_GET['del'])){
        	$this->_forward('delete','comment','admin');
        }
    }
    
    public function deleteAction(){
    	if(isset($_GET['comment-check']) && isset($_GET['del']) && !empty($_GET['comment-check'])){
    		$arrDel  = $_GET['comment-check'];
    		for ($i =0; $i <count($arrDel); $i ++){
    			$this->commentTbl->deleteComment($arrDel[$i]);
    		}
    	}
    	
    	$this->_redirect('/admin/comment');
    }
    /**
     * 
     * Lay ngay thang trong bang comment ra de dung cho viec loc 
     * du lieu
     * @return Array
     */
    private function getDateComment(){
    	//
    	// === Lay ngay thang ra de loc
    	$date = $this->commentTbl->fetchAllComments();
    	$mAr =array();

    	foreach ($date as $value) {
    		if(!empty($value['datecomment'])){
	    		$m = explode("/", $value['datecomment']);
//	    		
				$flag = false;
				foreach ($mAr as $ck) {
					if ($m[1] == $ck['mon'] && $m[2] == $ck['year']){
						$flag = true;
						break;
					}
				}
				
				if ($flag)	continue;
					
	    		$mAr[] = array(
	    					'mon' => $m[1],
	    					'year'=> $m[2]
	    				);
	    	}
    	}

    	return $mAr;
    }


}



